<template>
  <s-layout title="批量出售">

    <view class="card-wrap">
      <view class="ss-flex">
        <image src="@/static/img/inheritanceOfficer-1.png" mode=""></image>
        <view class="card-wrap-t">
          <view class="card-wrap-t1">汽车减震器</view>
          <view class="ss-flex">
            <view class="card-wrap-t4">藏品发售价：</view>
            <view class="card-wrap-t5">￥550.00</view>
          </view>
          <view class="ss-flex">
            <view class="card-wrap-t4">最新成交价：</view>
            <view class="card-wrap-t5">￥550.00</view>
          </view>
          <view class="ss-flex">
            <view class="card-wrap-t2">当前指导价：</view>
            <view class="card-wrap-t3">￥550.00</view>
          </view>
        </view>
      </view>
    </view>

    <view class="choose-wrap">
      <view class="choose-wrap-t ss-flex ss-row-between">
        <view class="ss-flex">
          <view class="choose-wrap-t1 ss-flex">
            <text>默认排序</text>
            <image src="@/static/img/public-icon-6.png"></image>
          </view>
          <view class="choose-wrap-t1 ss-flex">
            <text>全部</text>
            <image src="@/static/img/public-icon-6.png"></image>
          </view>
        </view>
        <view class="ss-flex">
          <view class="choose-wrap-t2">全选</view>
        </view>
      </view>
      <view class="choose-wrap-l">
        <checkbox-group >
          <view class="choose-wrap-l-item ss-flex ss-row-between">
            <view class="ss-flex ss-row-between">
              <view>412/500</view>
              <view class="choose-wrap-l-item-b">防囤</view>

            </view>
            <view class="ss-flex ss-row-between">
              <view>559.85（595.58）</view>
              <checkbox label="绿色"></checkbox>
            </view>

          </view>
        </checkbox-group>
      </view>
    </view>

    <!-- 底部 -->
    <su-fixed bottom :opacity="false" placeholder :noFixed="false" :index="200">
      <view class="detail-tabbar-slot">
        <view class="detail-tabbar-slot-h">
          <view class="detail-tabbar-slot-ht">寄售单价</view>
          <view class="detail-tabbar-slot-hc">
            <view class="detail-tabbar-slot-hc-item">
              <text>价格</text>
              <text>500</text>
            </view>
            <view class="detail-tabbar-slot-hc-item">
              <text>出售数量</text>
              <text>500</text>
            </view>
          </view>
        </view>
        <view class="ss-flex ss-row-between">
          <view class="detail-tabbar-slot-t">
            <view class="detail-tabbar-slot-ttext">
              <view class="detail-tabbar-slot-t1">可到账金额：</view>
              <view class="detail-tabbar-slot-t2">¥50000.00</view>
            </view>
          </view>
          <view class="detail-tabbar-slot-b">
            <button class="ss-reset-button buy-btn ui-Shadow-Main" @tap="state.showChooseBuy = true">
              发布
            </button>
          </view>
        </view>
      </view>
    </su-fixed>

    <s-popup-tips :show="state.showChooseBuy"
                  @buy="onBuy" @change="onBuyChange" @close="onClose" />
  </s-layout>
</template>

<script setup>
  import { computed, reactive, ref } from 'vue';
  import { onLoad } from '@dcloudio/uni-app';
  import sheep from '@/sheep';
  import { isEmpty } from 'lodash';

  const state = reactive({
    orderPayload: {},
    orderInfo: {},
    addressInfo: {},
    invoiceInfo: {},
    totalNumber: 0,
    showCoupon: false,
    couponInfo: [],
    showDiscount: false,
    showChooseBuy: false,
  });
  const checkboxValue1 = ref(false);

  // 立即兑换(立即兑换无需跳转收银台)
  const exchangeNow = computed(
    () => state.orderPayload.order_type === 'score' && state.orderInfo.pay_fee == 0,
  );

  function onClose() {
    state.showChooseBuy = false;
    sheep.$router.go('/pages/inherit/payment');
  }

  // 选择地址
  function onSelectAddress() {
    uni.$once('SELECT_ADDRESS', (e) => {
      changeConsignee(e.addressInfo);
    });
    sheep.$router.go('/pages/user/address/list');
  }

  // 更改收货人地址&计算订单信息
  async function changeConsignee(addressInfo = {}) {
    if (isEmpty(addressInfo)) {
      const {
        code,
        data,
      } = await sheep.$api.user.address.default();
      if (code === 1 && !isEmpty(data)) {
        addressInfo = data;
      }
    }
    if (!isEmpty(addressInfo)) {
      state.addressInfo = addressInfo;
      state.orderPayload.address_id = state.addressInfo.id;
    }
    getOrderInfo();
  }

  // 选择优惠券
  async function onSelectCoupon(e) {
    state.orderPayload.coupon_id = e || 0;
    getOrderInfo();
    state.showCoupon = false;
  }

  // 选择发票信息
  function onSelectInvoice() {
    uni.$once('SELECT_INVOICE', (e) => {
      state.invoiceInfo = e.invoiceInfo;
      state.orderPayload.invoice_id = e.invoiceInfo.id || 0;
    });
    sheep.$router.go('/pages/user/invoice/list');
  }

  // 提交订单/立即兑换
  function onConfirm() {
    if (!state.orderPayload.address_id && state.orderInfo.need_address === 1) {
      sheep.$helper.toast('请选择收货地址');
      return;
    }

    if (exchangeNow.value) {
      uni.showModal({
        title: '提示',
        content: '确定使用积分立即兑换?',
        cancelText: '再想想',
        success: async function(res) {
          if (res.confirm) {
            submitOrder();
          }
        },
      });
    } else {
      submitOrder();
    }
  }

  // 创建订单&跳转
  async function submitOrder() {
    const {
      code,
      data,
    } = await sheep.$api.order.create(state.orderPayload);
    if (code === 1) {
      // 更新购物车列表
      if (state.orderPayload.from === 'cart') {
        sheep.$store('cart').getList();
      }
      if (data.status === 'paid') {
        sheep.$router.redirect('/pages/pay/result', {
          orderSN: data.order_sn,
        });
      } else {
        sheep.$router.redirect('/pages/pay/index', {
          orderSN: data.order_sn,
        });
      }
    }
  }

  // 检查库存&计算订单价格
  async function getOrderInfo() {
    let {
      code,
      data,
    } = await sheep.$api.order.calc(state.orderPayload);
    if (code === 1) {
      state.totalNumber = 0;
      state.orderInfo = data;
      state.orderInfo.goods_list.forEach((item) => {
        state.totalNumber += item.goods_num;
      });
    }
  }

  // 获取可用优惠券
  async function getCoupons() {
    const {
      code,
      data,
    } = await sheep.$api.order.coupons(state.orderPayload);
    if (code === 1) {
      state.couponInfo = data;
    }
  }

  onLoad(async (options) => {
    // if (options.data) {
    // 	state.orderPayload = JSON.parse(options.data);
    // 	changeConsignee();
    // 	if (state.orderPayload.order_type !== 'score') {
    // 		getCoupons();
    // 	}
    // }
  });
</script>

<style lang="scss" scoped>
  .card-wrap {
    margin: 30rpx;
    padding: 20rpx;
    background: #1E1E1E;
    border-radius: 20rpx;

    image {
      width: 180rpx;
      height: 176rpx;
      margin-right: 35rpx;
    }

    .card-wrap-t {
      .ss-flex {
        margin-top: 15rpx;
        align-items: baseline;
      }

      .card-wrap-t1 {

        font-weight: 400;
        font-size: 28rpx;
        color: #FFFFFF;
      }

      .card-wrap-t2 {

        font-weight: 400;
        font-size: 24rpx;
        color: #45C9F0;
      }

      .card-wrap-t3 {

        font-weight: 400;
        font-size: 24rpx;
        color: #45C9F0;
      }

      .card-wrap-t4 {

        font-weight: 400;
        font-size: 20rpx;
        color: #D9D9D9;
      }

      .card-wrap-t5 {

        font-weight: 400;
        font-size: 20rpx;
        color: #D9D9D9;
      }
    }
  }

  .choose-wrap {
    margin: 30rpx;

    .choose-wrap-t {
      width: 100%;

      .choose-wrap-t1 {

        font-weight: 400;
        font-size: 32rpx;
        color: #FFFFFF;

        image {
          width: 15rpx;
          height: 8rpx;
          margin-left: 20rpx;
        }

        &:last-child {
          margin-left: 60rpx;
        }
      }

      .choose-wrap-t2 {

        font-weight: 400;
        font-size: 30rpx;
        color: #FFFFFF;
      }
    }

    .choose-wrap-l {
      .choose-wrap-l-item {
        background: #1E1E1E;
        border-radius: 20rpx;
        padding: 26rpx 0;
        margin-top: 22rpx;

        font-weight: 400;
        font-size: 28rpx;
        color: #FFFFFF;

        .ss-flex {
          width: 50%;
          margin: 0 20rpx;
        }

        .choose-wrap-l-item-b {
          width: 64rpx;
          height: 34rpx;
          line-height: 34rpx;
          text-align: center;
          background: linear-gradient(90deg, #45C9F0, #36F6AF);
          border-radius: 4rpx;

          font-weight: 400;
          font-size: 20rpx;
          color: #090909;
        }
      }
    }
  }

  :deep() {
    .uni-checkbox-input {
      background-color: var(--ui-BG-Main);
    }
  }

  @mixin flex($align-item:center,$justify-content:space-between) {
    display: flex;
    align-items: $align-item;
    justify-content: $justify-content;
  }

  .detail-tabbar-slot {
    padding: 40rpx 30rpx;

    .detail-tabbar-slot-h {
      .detail-tabbar-slot-ht {

        font-weight: 400;
        font-size: 32rpx;
        color: #F5F6F8;
      }

      .detail-tabbar-slot-hc {
        background: #1E1E1E;
        border-radius: 20rpx;
        margin: 30rpx 0;

        .detail-tabbar-slot-hc-item {
          padding: 30rpx 20rpx;
          @include flex();

          font-weight: 400;
          font-size: 30rpx;
          color: #FFFFFF;

          &:last-child {
            border-top: 1px solid #353535;
          }
        }
      }
    }

    .detail-tabbar-slot-t {
      @include flex();

      .detail-tabbar-slot-ttext {
        @include flex(center, none);

        .detail-tabbar-slot-t1 {

          font-weight: 400;
          font-size: 32rpx;
          color: #FFFFFF;
        }

        .detail-tabbar-slot-t2 {

          font-weight: 500;
          font-size: 42rpx;
          color: #FFFFFF;
        }
      }

      .detail-tabbar-slot-btext {
        margin-top: 15rpx;

        font-weight: 400;
        font-size: 32rpx;
        color: #888888;
      }
    }

    .detail-tabbar-slot-b {
      @include flex(center, none);

      .sold-btn {
        width: 230rpx;
        height: 90rpx;
        background: linear-gradient(90deg, #45C9F0, #36F6AF);
        border-radius: 45rpx;
        text-align: center;
        line-height: 90rpx;

        font-weight: 500;
        font-size: 32rpx;
        color: #090909;
      }

      .buy-btn {
        width: 180rpx;
        height: 75rpx;
        background: linear-gradient(90deg, #45C9F0, #36F6AF);
        border-radius: 38rpx;
        text-align: center;
        line-height: 75rpx;

        font-weight: 500;
        font-size: 32rpx;
        color: #090909;
        margin-left: 20rpx;
      }
    }
  }
</style>
